package licode.unisop.realm.adapter.web;

import licode.unisop.client.utils.PlatformCheck;
import licode.unisop.provider.info.SiAccountIn;
import licode.unisop.provider.info.SiIdentityIn;
import licode.unisop.provider.info.SiUserId;
import licode.unisop.provider.info.UserIdInfo;
import licode.unisop.provider.vo.SiUser;
import licode.unisop.realm.client.api.SiUserService;
import licode.unisop.realm.client.info.SiResetPasswordIn;
import licode.unisop.realm.client.info.SiUpdatePasswordIn;
import licode.unisop.unilite.result.UniReturnResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 适配RESTFUL接口访问，处理用户的注册和登录
 *
 * @author WR
 */
@RestController
@UniReturnResult
@RequestMapping("/user-api/v1/internal/user")
public class UserController {
    @Resource
    private HttpServletRequest request;

    @Resource
    private PlatformCheck platformCheck;

    @Resource
    private SiUserService siUserService;

    @PostMapping("/password/update")
    public void updatePassword(@RequestBody SiUpdatePasswordIn info) {
        platformCheck.checkModalToken(request);
        siUserService.updatePassword(info);
    }

    /**
     * 重置用户密码
     * @param info 账号信息
     */
    @RequestMapping(value = "/password/reset", method = RequestMethod.POST)
    public void resetPassword(@RequestBody SiResetPasswordIn info) {
        platformCheck.checkModalToken(request);
        siUserService.resetPassword(info);
    }

    @PostMapping("/save")
    public SiUser saveUser(@RequestBody SiUser user) {
        platformCheck.checkModalToken(request);
        return siUserService.saveUser(user);
    }

    @PostMapping("/account/save")
    public boolean saveAccount(@RequestBody SiAccountIn info) {
        platformCheck.checkModalToken(request);
        siUserService.saveAccount(info);
        return true;
    }

    @PostMapping("/identity/save")
    public boolean saveIdentity(@RequestBody SiIdentityIn info) {
        platformCheck.checkModalToken(request);
        siUserService.saveIdentity(info);
        return true;
    }

    @PostMapping("/login-time/update")
    public void updateLastLoginTime(@RequestBody UserIdInfo idInfo) {

    }

    /**
     * 更新用户缓存
     *
     * @param userId 用户ID信息
     */
    @PostMapping("/cache/update")
    void updateCache(@RequestBody SiUserId userId) {
        platformCheck.checkModalToken(request);
        siUserService.updateCache(userId);
    }
}
